//
//  JKCommentSectionHeaderView.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/12.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKCommentSectionHeaderView: JKBaseHeaderFooterView {
    
    
    static let commentSectionHeaderID = "sectionHeader"

    /** 类方法 初始化 */
    public class func headerViewWithTableView(tableView: UITableView) -> JKCommentSectionHeaderView {
        
        var sectionHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: commentSectionHeaderID) as? JKCommentSectionHeaderView
        
        if (sectionHeaderView == nil) {//缓存池中没有，自己创建
            
            sectionHeaderView = JKCommentSectionHeaderView(reuseIdentifier: commentSectionHeaderID)
        }
        
        return sectionHeaderView!
    }
    
    /** 评论数量text */
    var cmtCountText: String?{
        
        didSet{
           
            cmtCountLabel.text = cmtCountText
        }
    }
    
    /** unfold按钮是否隐藏 */
    var unfoldButtonHidden = false {
        
        didSet{
            
            unfoldButton.isHidden = unfoldButtonHidden
        }
    }
    
    /** unfoldButton添加事件 */
    public func unfoldButtonAddTarget(target: Any, action: Selector) {
        
        unfoldButton.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
    }
    
    /** 刷新表格后恢复unfoldButton的选中状态 */
    public func resetUnfoldButtonSelected(shortCmtUnfolded: Bool) {
        
        unfoldButton.isSelected = shortCmtUnfolded
    }

    
    // MARK: - 私有属性
    
    private lazy var cmtCountLabel: UILabel = {
        
        let cmtCountLabel = UILabel()
        
        cmtCountLabel.textColor = JKSameRGBColor(53)
        cmtCountLabel.font = UIFont.boldSystemFont(ofSize: 16)
        contentView.addSubview(cmtCountLabel)
        
        return cmtCountLabel
    }()
    
    private lazy var unfoldButton: UIButton = {
        
        let unfoldButton = UIButton(type: UIButtonType.custom)
        
        unfoldButton.setImage(UIImage(named: "Comment_Icon_Fold"), for: UIControlState.normal)
        unfoldButton.setImage(UIImage(named: "Comment_Icon_Fold_Selected"), for: UIControlState.selected)
        unfoldButton.isHidden = true
        contentView.addSubview(unfoldButton)
        
        return unfoldButton
    }()
    
    /** 分隔线 */
//    private var separatorView: UIView?
    
    
    // MARK: - 初始化
    
    override func initialization() {
        super.initialization()
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.cmtCountLabel.height = self.height
        self.cmtCountLabel.width = 150
        self.cmtCountLabel.x = 10
        self.cmtCountLabel.y = 0
        
        self.unfoldButton.width = self.height
        self.unfoldButton.height = self.height
        self.unfoldButton.x = self.width - 10 - self.unfoldButton.width
        self.unfoldButton.y = 0
        
        //    self.separatorView.frame = CGRectMake(0, self.height - 0.5, self.width, 0.5)
    }
}

extension JKCommentSectionHeaderView {
    
    override func updateSkin() {
        super.updateSkin()
        
        cmtCountLabel.textColor = (skinModel == JKDaySkinModelValue) ? UIColor.black : JKSameRGBColor(186)
        
        contentView.backgroundColor = (skinModel == JKNightSkinModelValue) ? JKSameRGBColor(53) : JKSameRGBColor(233)
    }
}
